package cn.tedu.supermarket.service;

import org.apache.commons.mail.HtmlEmail;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    private String captcha;
    public String getCaptcha() { return captcha; }

    public void email(String mailIndex) {

        try {
            System.out.println(mailIndex);
            HtmlEmail htmlEmail=new HtmlEmail();
            htmlEmail.setHostName("smtp.qq.com");
            htmlEmail.setCharset("utf-8");
            htmlEmail.addTo(mailIndex);//目标邮箱地址
            htmlEmail.setFrom("maoxu1979@qq.com","邮箱验证");//你的邮箱地址
            htmlEmail.setAuthentication("maoxu1979@qq.com","bplpoonvoecbfebj");//你的邮箱地址和你的stmp密码
            int a = (int)((Math.random()*9+1)*100000);
            captcha = String.valueOf(a);
            System.out.println(captcha);
            htmlEmail.setTextMsg("验证码");
            htmlEmail.setMsg("<!DOCTYPE html>\n"+
                    "<html lang='en'>\n"+
                    "<head>\n"+
                    "<meta charset='UTF-8'>\n"+
                    "<title>邮箱验证</title>\n"+
                    "</head>\n"+
                    "<body>\n"+
                    "<h3>亲爱的用户:</h3>\n"+
                    "<P>您好！您正在进行邮箱验证，本次请求的验证码为:"+
                    "<span style='color: #ff7518;font-size: 20px;font-weight: 600'>"+captcha+"</span>"+
                    "(为了保障您帐号的安全性,请在10分钟内完成验证)</P>\n"+
                    "</body>\n"+
                    "</html>\n");//内容最好不要太简单了，不然容易进垃圾邮箱
            htmlEmail.send();
            System.out.println("发送成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
